/**
 * @file unicode_compat.h
 * @brief Unicode compatibility fixes
 */

#ifndef UNICODE_COMPAT_H
#define UNICODE_COMPAT_H

#ifdef _WIN32
#include <windows.h>
#endif

#include "stlstrings.h"
#include <wx/string.h>

// 兼容性包装函数
inline bool FileExistsCompat(const CString& path) {
#ifdef _WIN32
    wxString wxpath(path.c_str(), wxConvUTF8);
    DWORD attr = GetFileAttributes(wxpath.wc_str());
    return (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY));
#else
    return access(path.c_str(), F_OK) == 0;
#endif
}

inline bool DirExistsCompat(const CString& path) {
#ifdef _WIN32
    wxString wxpath(path.c_str(), wxConvUTF8);
    DWORD attr = GetFileAttributes(wxpath.wc_str());
    return (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY));
#else
    struct stat st;
    return (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
#endif
}

inline bool MakeDirCompat(const CString& path) {
#ifdef _WIN32
    wxString wxpath(path.c_str(), wxConvUTF8);
    return CreateDirectory(wxpath.wc_str(), NULL);
#else
    return mkdir(path.c_str(), 0755) == 0;
#endif
}

inline CString GetCurrentDirCompat() {
#ifdef _WIN32
    wchar_t buffer[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, buffer);
    return CString(wxString(buffer).ToUTF8());
#else
    char buffer[PATH_MAX];
    return CString(getcwd(buffer, PATH_MAX));
#endif
}

#endif // UNICODE_COMPAT_H
